﻿using Abp.Web.Models;
using MyCompanyName.AbpZeroTemplate.SystemSetting;
using MyCompanyName.AbpZeroTemplate.SystemSetting.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace MyCompanyName.AbpZeroTemplate.Web.Areas.Mpa.Controllers
{
    public class SystemController : Controller
    {
        private readonly ISystemSettingService _systemSettingAppService;
        public SystemController(ISystemSettingService systemSettingAppService)
        {
            _systemSettingAppService = systemSettingAppService;
        }
        // GET: Mpa/Syatem
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 异步获得系统设置的DataTable数据
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        /// 
        [DontWrapResult]
        public ActionResult GetSystemSettingDataTable(int offset, int limit, string Filter1SearchName)
        {
            //参数
            string Filter1SearchNamea = Request["Filter1SearchName"];
            string SystemSettingName = Filter1SearchName;
            int PageSize = limit;
            int PageNumber = (offset / limit) + 1;
            //获得数据
            var result = _systemSettingAppService.GetAgentDtoList(SystemSettingName, PageSize, PageNumber);
            //var resultGet = result.Skip(offset).Take(limit).ToList();
            object data = new { total = result.Items.Count, rows = result.Items };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建和新增
        /// </summary>
        /// <param name="AgentId"></param>
        /// <returns></returns>
        public ActionResult CreteOrUpdate(int SystemSettingId)
        {
            SystemSettingDto agent = new SystemSettingDto();
            if (SystemSettingId != 0)
            {
                agent = _systemSettingAppService.GetSystemSettingId(SystemSettingId);
            }
            return View(agent);
        }

        /// <summary>
        /// 提交系统设置创建的表单
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult SystemSettingCreateForm(CreateSystemSettingInput input)
        {
            bool IsSuccess = true;
            string ErrorMsg = "";

            try
            {
                _systemSettingAppService.CreateSystemSetting(input);
            }
            catch (Exception ex)
            {
                IsSuccess = false;
                ErrorMsg = "创建失败" + ex.Message;
            }
            return Json(
                new
                {
                    status = IsSuccess,
                    errormsg = ErrorMsg
                });
        }

        /// <summary>
        /// 提交系统设置创建的表单
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult SystemSettingUpdateForm(UpdateSystemSettingInput model)
        {
            bool IsSuccess = true;
            string ErrorMsg = "";

            try
            {
                var result = _systemSettingAppService.UpdateSystemSetting(model);
                if (result == true)
                {
                    IsSuccess = true;
                    ErrorMsg = "修改成功";
                }
                else
                {
                    IsSuccess = false;
                    ErrorMsg = "修改失败";
                }
            }
            catch (Exception ex)
            {
                IsSuccess = false;
                ErrorMsg = "修改失败" + ex.Message;
            }
            return Json(
                new
                {
                    status = IsSuccess,
                    errormsg = ErrorMsg
                });
        }
    }
}